import modelExtend from 'dva-model-extend'
import { pageModel } from '@/common/model/BaseModel'
import { getApproveList, getApproveDetail, consentApprove } from '../services/approve'

export default modelExtend(pageModel, {
  namespace: 'approve',
  state: {
    showProps: false,
    currentItem: {},
    //
    showApproveModal: false,
    // 审批单中的 资产
    currentProps: [],
    // 审批单步骤数据
    currentSteps: [],
    formValues: {},
  },
  effects: {
    *fetchList({ payload }, { call, put }) {
      console.log('fetchApproveList')
      const res = yield call(getApproveList, payload || {})
      yield put({
        type: 'updateState',
        payload: {
          list: res.data.data,
          pagination: {
            showSizeChanger: false,
            showQuickJumper: true,
            showTotal: total => `共 ${total} 条`,
            total: res.data.resourceSize,
            current: res.data.current,
            pageSize: res.data.size,
          },
          formValues: payload,
        },
      })
    },

    *fetchProps({ payload }, { call, put }) {
      const { dataKey, ...args } = payload
      const res = yield call(getApproveDetail, args)
      //
      const currentProps = res.data[dataKey]
      yield put({
        type: 'updateState',
        payload: {
          currentProps,
          currentSteps: res.tag.data || [],
        },
      })
    },

    *consent({ payload }, { call, put }) {
      const res = yield call(consentApprove, payload)
      if (res && res.success) {
        yield put({
          type: 'updateState',
          payload: {
            currentItem: {},
            showApproveModal: false,
          },
        })
        yield put({
          type: 'fetchList',
        })
      }
    },
  },
})
